<template>
	<!-- 管理员转库 -->
	<view>
		<view class="boxcent">
			<view class="box1">
				<text class="text2 txt">日期:</text>
				<view class="datetime">
					<uni-datetime-picker type="date" v-model="production"></uni-datetime-picker>
				</view>
			</view>
			<view class="br"></view>
			<view class="box1">
				<text class="text txt">类型:</text>
				<view class="radio">
					<u-radio-group  v-model="typevalue">
						<u-radio :name="names[0]" :disabled="true">{{names[0]}}</u-radio>
						<u-radio :name="names[1]" :disabled="true">{{names[1]}}</u-radio>
						<u-radio :name="names[2]">{{names[2]}}</u-radio>
					</u-radio-group>
				</view>
			</view>
			<view class="br"></view>
			<view class="box1">
				<text class="text txt">说明:</text>
				<view class="radio1">
					<u-radio-group  v-model="notevalue">
						<u-radio  v-for="(item, index) in options" :key="index" :name="item.nonname">
							{{item.nonname}}
						</u-radio>
					</u-radio-group>
				</view>
			</view>
			<view class="br"></view>
			<view class="box1" @click="stopage">
				<text class="text txt">商品名称:</text>
				<input class="text1" type="text" v-model="stoname" placeholder="请选择商品">
				<image class="img" src="../../../static/paimg/跳转.png"></image>
			</view>
			<view class="br"></view>
			<view class="box1">
				<text class="text txt">规格:</text>
				<input class="text1" type="text" disabled="false" v-model="stospe" placeholder="请输入规格">
			</view>
			<view class="br"></view>
			<view class="box1" @click="stopage">
				<text class="text txt">商品编号:</text>
				<input class="text1" type="text" v-model="stoid" placeholder="请选择商品">
				<image class="img" src="../../../static/paimg/跳转.png"></image>
			</view>
			<view class="br"></view>
			<view class="box1">
				<text class="text txt">数量:</text>
				<input class="text1"  @input="get" type="number" v-model="oldstonum" placeholder="请输入商品数量">
			</view>
			<view class="br"></view>
			<view class="box1">
				<text class="text txt">单价:</text>
				<input class="text1" disabled="false" type="number" v-model="purchase_price" placeholder="请输入商品单价">
			</view>
			<view class="br"></view>
			<view class="box1">
				<text class="text txt">总金额:</text>
				<input class="text1"type="number" disabled="false" v-model="amount" placeholder="商品总金额">
			</view>
			<view class="br"></view>
			<view class="box1" @click="suppage">
				<text class="text txt">供应商:</text>
				<input class="text1" type="text" disabled="false" v-model="supname" placeholder="请选择供应商">
				<image class="img" src="../../../static/paimg/跳转.png"></image>
			</view>
			<view class="br"></view>
			<view class="box1">
				<text class="text txt">资金账户:</text>
				<input class="text1" type="text" disabled="false" v-model="supaccount" placeholder="请选择供应商">
			</view>
			<view class="br"></view>
			<view class="box1" @click="warpage">
				<text class="text txt">原仓库名:</text>
				<input class="text1" type="text" disabled="false" v-model="wname" placeholder="请选择仓库名称">
				<image class="img" src="../../../static/paimg/跳转.png"></image>
			</view>
			<view class="br"></view>
			<view class="box1" @click="newwarpage">
				<text class="text txt">新仓库名:</text>
				<input class="text1" type="text" disabled="false" v-model="newwname" placeholder="请选择仓库名称">
				<image class="img" src="../../../static/paimg/跳转.png"></image>
			</view>
			<view class="br"></view>
			<view class="box1">
				<text class="text txt">经办人:</text>
				<input class="text1" type="text" disabled="false" v-model="whentryname" placeholder="经办人">
			</view>
			<view class="br"></view>
			<view class="box1">
				<text class="text txt">详情:</text>
				<input class="text1" type="text" v-model="title" placeholder="空">
			</view>
		</view>
		<view class="btnbox">
			<button @click="inst">保存</button>
		</view>
	</view>
</template>

<script>
	import getDateTime from '../../../until/getdateTime.js'
	export default {
		data() {
			return {
				options:{},purchase_price:'',retail_price:'',
				names:['入库','出库','转库'],wid:'',productions:'',expirations:'',newwid:'',newwname:'',
				production:'1900-01-01',typevalue:'转库',notevalue:'',stoid:'',stoname:'',stospe:'',
				num:'',purchase_price:'',amount:'',supname:'',supaccount:'',wname:'',newwname:'',supid:'',
				whentryname:'',	title:'',oldstonum:'',unit:''
			}
		},
		onLoad(){
			this.getdata();
			this.production = getDateTime.dateTimeStr().substring(0, 10);
		},
		onShow:function(){
			var pages = getCurrentPages();
			var currPage = pages[pages.length - 1]; //当前页面
			var name=currPage.name;//商品名称
			var id = currPage.id;//商品编号
			var spe = currPage.spe;//商品规格
			var price = currPage.price;//商品进价
			var retail_price = currPage.retail;//商品售价
			var produc = currPage.produc;//商品生产日期
			var expira = currPage.expira;//商品保质日期
			var sunit = currPage.sunit;//商品单位
			if(name !==undefined){
				this.stoname = name
				this.stoid = id
				this.stospe = spe
				this.purchase_price =price
				this.productions = produc;
				this.expirations = expira;
				this.retail_price = retail_price
				this.unit = sunit
			}
			var sname=currPage.sname;//供应商名称
			var saccount = currPage.saccount;//供应商银行账号
			var spid = currPage.sid;//供应商编号
			if(sname !==undefined){
				this.supname = sname
				this.supid = spid
				this.supaccount = saccount
			}
			var warname = currPage.warname;//仓库名称
			var warid = currPage.warid;//仓库编号
			if(warname !==undefined){
				this.wname = warname
				this.wid = warid
			}
			var warnewname = currPage.warnewname;//新仓库名称
			var warnewid = currPage.warnewid;//新仓库编号
			if(warnewname !==undefined){
				this.newwname = warnewname
				this.newwid = warnewid
			}
		},
		methods: {
			get(){
				this.amount =this.oldstonum*this.purchase_price
			},
			async getdata(){
				const res1 = await this.Request({
					url:'api/public/grttype',
					method:'POST',
					data:{notype:this.typevalue}
				})
				this.options = res1.data
				var account = uni.getStorageSync('account');
				const resname = await this.Request({
					url:'api/employee/emp_getname',
					method:'POST',
					data:{account:account}
				})
				this.whentryname = resname.data[0].empname
			},
			stopage(){
				uni.navigateTo({
					url:'/pages/public/choose/choosegoods'
				})
			},
			suppage(){
				if(this.stoname==''){
					uni.showToast({
						icon:'error',
						title: '请选择商品'
					})
				}else{
					uni.navigateTo({
						url:'/pages/public/choose/choosesup?stoname='+this.stoname+'&typevalue='+this.typevalue
					})
				}
			},
			warpage(){
				if(this.stoname=='' || this.supid==''){
					uni.showToast({
						icon:'error',
						title: '请选择商品和供应商'
					})
				}else{
					uni.navigateTo({
						url:'/pages/public/choose/choosewar?stoname='+this.stoname+'&supid='+this.supid+'&typevalue='+this.typevalue
					})
				}
			},
			newwarpage(){
				uni.navigateTo({
					url:'/pages/public/choosenewwar'
				})
			},
			async inst(){
				if(this.oldstonum.replace(/\s*/g,"").length == 0 || this.stoname == '' ||
					this.notevalue == '' || this.supname == '' ||
					this.wname == '' || this.newwname == ''){
					uni.showToast({
						icon:"error",
						title:'内容不可为空'
					})
				}else {
					// 转库=>查询所选择的商品信息(数量,id)
					const res1 = await this.Request({
						url:'api/stock/getstoid',
						method:'POST',
						data:{name:this.stoname,supplier_id:this.supid,wid:this.wid}
					})
					//原仓库的商品数量
					var oldnum = res1.data[0].num;
					var id = res1.data[0].id;
					var newstonum = parseInt(oldnum)-parseInt(this.oldstonum)
					// 转库=>查询所选择的商品信息=>判断商品在新仓库是否存在
					const res2 = await this.Request({
						url:'api/stock/getstoid',
						method:'POST',
						data:{name:this.stoname,supplier_id:this.supid,wid:this.newwid}
					})
					if(res2.data.length != 0){
						var newnum =  parseInt(res2.data[0].num) + parseInt(this.oldstonum)
						// 转库=>商品存在=>修改商品现位置数量
						const res3 = await this.Request({
							url:'api/stock/setstonum',
							method:'POST',
							data:{num:newnum,id:res2.data[0].id}
						})
						// 转库=>商品存在=>修改商品原位置数量
						const res4 = await this.Request({
							url:'api/stock/setstonum',
							method:'POST',
							data:{num:newstonum,id:this.stoid}
						})
						if(res4.data.affectedRows=1){
							//转库=>商品存在=>修改商品原位置数量=>添加出库记录
							const res = await this.Request({
								url:'api/record/stockadd',
								method:'PUT',
								data:{data:this.production,type:this.typevalue,note:this.notevalue,stoname:this.stoname,
									  stospe:this.stospe,stoid:this.stoid,num:this.oldstonum,purchase_price:this.purchase_price,
									  amount:this.amount,supid:this.supid,supname:this.supname,bank_account:this.supaccount,
									  wname:this.wname,nowwar:this.newwname,whentryname:this.whentryname,title:this.title}
							})
							if(res.data.affectedRows=1){
								uni.showToast({
									title: '转库成功'
								})
								setTimeout(() => {
									uni.reLaunch({
										url:'/pages/super/sup-information/sup-information'
									})
								},500)
							}
						}
					}else{
						// 转库=>添加出库记录=>商品不存在=>添加商品
						var src = 'http://localhost:3000/static/upload/63b5f47c48c132eaced5f5888f47727a.png';
						const res5 = await this.Request({
							url:'/api/super/stockadd',
							method:'PUT',
							data:{img:src,name:this.stoname,spe:this.stospe,num:this.oldstonum,
							wid:this.newwid,alarmnum:5,production_date:this.productions.substring(0,10),
							expiration_date:this.expirations.substring(0,10),supplier_id:this.supid,
							purchase_price:this.purchase_price,retail_price:this.retail_price,unit:this.unit}
						})
						if(res5.data.affectedRows=1){
							//转库=>添加出库记录=>商品不存在=>添加商品=>添加出库记录
							const res = await this.Request({
								url:'api/record/stockadd',
								method:'PUT',
								data:{data:this.production,type:this.typevalue,note:this.notevalue,stoname:this.stoname,
									  stospe:this.stospe,stoid:this.stoid,num:this.oldstonum,purchase_price:this.purchase_price,
									  amount:this.amount,supid:this.supid,supname:this.supname,bank_account:this.supaccount,
									  wname:this.wname,nowwar:this.newwname,whentryname:this.whentryname,title:this.title}
							})
							if(res.data.affectedRows=1){
								uni.showToast({
									title: '转库成功'
								})
								setTimeout(() => {
									uni.reLaunch({
										url:'/pages/super/sup-information/sup-information'
									})
								},500)
							}
						}
					}
				}
			}
		}
	}
</script>

<style lang="scss">
	.titlebox{
		width: 100%;
		height: 30px;
		background-color: #efefef;
		display: flex;
		border-bottom: 1px solid #ccc;
		.text{
			margin: 5px 0 5px 5px;
			font-size: 110%;
			font-weight: 600;
			color: #008300;
		}
	}
	.boxcent{
		// margin-top: 10px;
		border-top: 2px solid #ccc;
		border-bottom: 2px solid #ccc;
		border-radius: 10px;
		position: relative;
		.br{
			height: 1px;
			margin-left: 10%;
			background: #a5a5a5;
		}
		.box1{
			display: flex;
			margin-top: 10px;
			margin-bottom: 10px;
			
			width: 100%;
			.txt{
				position: absolute;
				font-weight: 600;
				font-size: 110%;
			}
			.radio{
				position: relative;
				margin-left: 150px;
			}
			.radio1{
				position: relative;
				margin-left: 93px;
			}
			.img{
				position: absolute;
				height: 20px;
				width: 20px;
				margin-left: 355px;
			}
			.datetime{
				margin-left: 150px;
				width: 180px;
			}
			.text{
				margin-left: 10%;
			}
			.text1{
				margin-left: 170px;
			}
			.text2{
				margin-left: 10%;
				margin-top: 10px;
			}
			.text3{
				margin-left: 170px;
			}
		}
	}
	.btnbox{
		margin-top: 15px;
		margin-bottom: 60px;
		button{
			width: 50%;
			background-color: #ff5500;
			color: #ffffff;
		}
	}
</style>
